package com.cosmic.pizza.dao.Impl;

import com.cosmic.pizza.dao.AddressDao;
import com.cosmic.pizza.pojo.Address;
import com.cosmic.pizza.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class AddressDaoImpl implements AddressDao {
    private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Address> findAddressList(int uid, int start, int pageSize) {
        String sql = "select * from tab_address where uid = ? limit ?,?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Address.class), uid, start, pageSize);
    }

    @Override
    public List<Address> findAddressList(int uid) {
        String sql = "select * from tab_address where uid = ?";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Address.class), uid);
    }

    @Override
    public int deleteByAid(int add_id) {
        String sql = "delete from tab_address where add_id = ?";
        return jdbcTemplate.update(sql, add_id);
    }

    @Override
    public int setDefaultByAid(int add_id) {
        String sql1 = "update tab_address set default_ad = 'N' where add_id = (select * from (select add_id from tab_address where default_ad = 'Y') a);";
        String sql2 = "update tab_address set default_ad = 'Y' where add_id = ?";
        return jdbcTemplate.update(sql1) & jdbcTemplate.update(sql2, add_id);
    }

    @Override
    public int insertAddress(Integer uid, Address address) {
        String sql = "insert into tab_address (address_detail, name, uid, sex, telephone) values(?,?,?,?,?)";
        return jdbcTemplate.update(sql, address.getAddress_detail(), address.getName(), uid, address.getSex(), address.getTelephone());
    }

    @Override
    public String findAddressById(int add_id) {
        String sql = "select address_detail from tab_address where add_id = ?";
        List<Address> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Address.class), add_id);
        return list.get(0).getAddress_detail();
    }

    @Override
    public String findNameById(int add_id) {
        String sql = "select name from tab_address where add_id = ?";
        List<Address> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Address.class), add_id);
        return list.get(0).getName();
    }

    @Override
    public int findAddressId(Integer uid) {
        String sql = "select add_id from tab_address where uid = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, uid);
    }

    @Override
    public Address findDefaultAddressByUid(Integer uid) {
        String sql = "select * from tab_address where uid = ? and default_ad = 'Y'";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Address.class), uid);
    }

    @Override
    public int findCountByUid(int uid) {
        String sql = "select count(*) from tab_address where uid = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, uid);
    }

    @Override
    public Address findAddressByAid(Integer add_id) {
        String sql = "select * from tab_address where add_id = ?";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Address.class), add_id);
    }

    @Override
    public int updateAddress(Address address) {
        String sql = "update tab_address set name = ?,address_detail = ?,sex = ?,telephone = ? where add_id = ?";
        return jdbcTemplate.update(sql, address.getName(), address.getAddress_detail(), address.getSex(), address.getTelephone(), address.getAdd_id());
    }
}
